
<?php
header('Content-type:text/html; charset=utf-8');

function authenticate()
{
    header('WWW-Authenticate: Basic realm="Basic authentication"');
    header('HTTP/1.1 401 Authorization Required');
    echo "Для доступа к ресурсу требуется ввести правильное имя и пароль\n";
    exit;
}

if (!isset($_SERVER['PHP_AUTH_USER']) ||
        ($_GET['var1'] != $_SERVER['PHP_AUTH_USER']))
{
    authenticate();
}
if ($_SERVER['REQUEST_METHOD'] != 'GET')
{
    header("HTTP/1.1 404  Not supported");
    echo "Not supported";
    return;
}




//if we end up here,  must be a GET method... fall through
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta content="text/html; charset=utf-8"
              http-equiv="content-type">
              <?php
              echo '<title>Пользователь ' . $_SERVER['PHP_AUTH_USER'] . ' коллекция</title>';
              ?>    </head>
    <body style="background-color: #5b5b5b; font-family: Comic Sans MS;">
        <div style="width: 600px; border: 1px solid #999; border-top: 0 ; margin: 20px auto; background-color: white; padding: 20px;
             border-top-left-radius:10px; border-top-right-radius: 10px; border-bottom-left-radius: 10px; border-bottom-right-radius: 10px;">    


            <b>Добро пожаловать, <?php echo $_SERVER['PHP_AUTH_USER'] ?></b><br>

            <?php

 			define( "DB_SERVER",    getenv('OPENSHIFT_MYSQL_DB_HOST') );
			define( "DB_USER",      getenv('OPENSHIFT_MYSQL_DB_USERNAME') );
			define( "DB_PASSWORD",  getenv('OPENSHIFT_MYSQL_DB_PASSWORD') );
			define( "DB_DATABASE",  getenv('OPENSHIFT_APP_NAME') );

            mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD) or die('cant connect to database');
            @mysql_select_db(DB_DATABASE) or die("Unable to select database");

            printf("<div>\n");
			if ( !empty( $_GET['var2'])  )  printf("<button onclick=\"delboard()\" id='delete'>Удалить</button>\n");
            echo ' Коллекция : ' . $_GET['var1'] . '/' . $_GET['var2'] . '<br>';
            printf("<img src=\"/html/back.gif\" alt=\"[DIR]\">&nbsp;&nbsp;<a href=\"../\">Родительский каталог</a><br>\n ");
            printf("</div>\n");
            ?>
            <div id="div1">
            </div>
        </div>
    </body>
</html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="Javascript/mchp.js" type="text/javascript"></script>
<script type="text/javascript">
    
    var URL = "";
    //var URL = "http://54.218.57.68";
    //var URL = "http://54.218.30.227";

    function delboard()
    {   
        var r = confirm("Уверены ?");
        if (r == false)
        {
            return;
        }
        newAJAXCommand(URL+'/xml/<?php echo $_GET['var1']; ?>/<?php echo $_GET['var2']; ?>/<?php echo $_GET['var3']; ?>/',"DELETE",refresh,0);
        //window.location = "../";//refresh page
    }
    function refresh()
    {
        window.location = "../";//refresh page
    }
</script>

<script>
//  JQuery script
    
    $.ajax({
//                    url: 'index.php?directory=' + foldertodelete,
        //url: './',
        url: URL+'/xml/<?php echo $_GET['var1']; ?>/<?php echo $_GET['var2']; ?>/<?php echo $_GET['var3']; ?>/',
        type: 'GET',
        success: function(result) {
            // Do something with the result
            //alert("Data: " + result);
            var i = 0, value;
            do {
                try {
                    value = result.getElementsByTagName("item")[i].firstChild.nodeValue;
                    //document.write (value);
                    var para = document.createElement("a");
                    var node = document.createTextNode(value);
                    para.appendChild(node);

                    var element = document.getElementById("div1");
                    element.appendChild(para);
                    
                    // [i+1] the +1 is becasue this is the second <a> tag.  The first is "Parent folder"
                    var h1 = document.getElementsByTagName("a")[i + 1]; 
                    var att = document.createAttribute("href");
                    att.value = "./" + value;
                    h1.setAttributeNode(att);

                    element.appendChild(document.createElement("br"))
                } catch (err)
                {
                    return null;
                }
            } while (++i)


            //window.location = "./";//refresh page
        },
        statusCode: {
            404: function() {
                alert("page not found");
            }
        }

    });
</script>
